namespace LocalBoard.Web
{
	using System;
	using System.Collections;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using LocalBoard.Domain;
	using LocalBoard.Controller;
	using LocalBoard.ServerControls;

	/// <summary>
	/// 	Summary description for MessageListView. -The Client asks the Singleton to get its unique instance .  It accesses a Singleton instance solely through Singleton's Instance operation.
	/// </summary>
	public partial class MessageListView : MessageUserControl
	{
		private MessageListViewType _messageListType = MessageListViewType.BoardMessageListView;

		public string MessageListType
		{
			get {return _messageListType.ToString();}
			set {_messageListType = value == null || value.Length == 0 ? MessageListViewType.BoardMessageListView : (MessageListViewType)Enum.Parse(typeof(MessageListViewType), value, true);}
		}

		private int _numOfDaysToBeOld = 3;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(GetMessageId() != 0)
				SetCurrentMessageId(GetMessageId());
			msgList.MessageList = boardController.GetMessageListForCurrentPage();
			msgList.CurrentMessageId = _currentMessageId;
			msgList.MessageListType = _messageListType.ToString();
			msgList.ExpandAll = true;
			DataBind();
		}

		public void SetCurrentMessageId(int messageId)
		{
			_currentMessageId = messageId;
		}
		protected string GetMessageNo(object messageNo)
		{
			int number = Convert.ToInt32(messageNo);
			if(number > 0)
				return number.ToString();
			else
				return "&nbsp;";
		}

		protected string GetMessageSubject(string subject, ArrayList shortMemos)
		{
			string numberOfReply = (shortMemos != null && shortMemos.Count > 0)?" <b>(" + shortMemos.Count.ToString() + ")</b>":"&nbsp;";
			return subject + numberOfReply;
		}

		protected string GetMessageNumberOfView(object message)
		{
			Message msg = message as Message;
			if(msg != null)
				return msg.Hits.ToString();
			else
				return "0";
		}

		protected string GetMessagePostedDate(object message)
		{
			Message msg = message as Message;
			if(msg != null)
				return msg.Date.ToString("mm/dd/yyyy");
			else
				return "&nbsp;";
		}

		protected string HideOrShowIndicator(object messageId)
		{
			if(_currentMessageId == Convert.ToInt32(messageId))
				return "";
			else
				return "hidden";
		}

		protected string GetUrl(object currentPageUrl, object messageId)
		{
			if(_currentMessageId == Convert.ToInt32(messageId))
				return "";
			return currentPageUrl.ToString() + "&message=" + messageId.ToString();
		}

		protected string GetMessageUserId(object message)
		{
			Message msg = message as Message;
			if(msg != null)
				return msg.PostedByUserId;
			else
				return "unknown";
		}

		protected string GetMessageUserName(object message)
		{
			Message msg = message as Message;
			if(msg != null)
				return msg.PostedByUserName;
			else
				return "unknown";
		}

		protected string CurrentMessageUrl(object messageId, object boardId, object clubId)
		{
			return ClubPage.AddClubBoardMessageQueryStringToUrl("SeeBoard.aspx", clubId.ToString(), boardId.ToString(), messageId.ToString());
		}

		protected string GetBoardName(object boardId, object isChild)
		{
			if(bool.Parse(isChild.ToString()))
				return "";
			int convertedBoardId = 0;
			try
			{
				convertedBoardId = Convert.ToInt32(boardId);
			}
			catch
			{}

			if(convertedBoardId == 0)
				return "";
			else
			{
				string boardName = clubController.GetBoard(convertedBoardId).Name;
				return boardName.Length < 10 ? boardName : boardName.Substring(0, 10);
			}
		}

		protected string HideOrShowNewIcon(object messagePostedDate)
		{
			if((DateTime.Now - DateTime.Parse(messagePostedDate.ToString())).Days < _numOfDaysToBeOld)
				return "";
			else
				return "hidden";
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
